var amqp = require('amqplib/callback_api');
var levels = ['debug', 'info', 'error'];
var level = process.argv[2];
if (level >= 0)
	console.log("  [x] bind %s", levels[level]);
else
	console.log(" [x] bind all");

amqp.connect('amqp://localhost', function(err, conn) {
	conn.createChannel(function(err, ch) {
		var ex = 'direct_logs';
		ch.assertExchange(ex, 'direct', {
			durable: false
		});

		ch.assertQueue('', {
			exclusive: true
		}, function(err, q) {
			// bind queue and exchange
			if (level >= 0)
				ch.bindQueue(q.queue, ex, levels[level]);
			else
				levels.forEach(function(item) {
					ch.bindQueue(q.queue, ex, item);
				})

			ch.consume(q.queue, function(msg) {
				console.log(" [x] %s", msg.content.toString());
			}, {
				noAck: true
			});
		});
	});
});

// node c.js 2
// then
// node c.js -1
// then 
// node p.js